<?php
namespace Admin\Controller;

class MovieController extends AdminController
{
	//首页
	public function index()
	{
		/*$data = M('movie')->field('id,cid,name,language,country,duration,showdate,director,drama,display')->select();*/
		/*var_dump($data);
		exit;*/
		if (!empty($_POST)) {
			/*var_dump($_POST); exit;*/
			$list = $_POST['name'];
			$data['name'] = array('like','%'.$list.'%'); 
			//var_dump($data);
			
			/*var_dump($data);exit;*/
			$count = M('movie')->where($data)->count();
			$page = new \Think\Page($count,8);
			$show = $page->show();
			$list = M('movie')->where($data)->limit($page->firstRow.','.$page->listRows)->select();
		} else {
			$count = M('movie')->count();
			$page = new \Think\Page($count,8);
			$show = $page->show();
			$list = M('movie')->limit($page->firstRow.','.$page->listRows)->order('id asc')->select();
		}
		/*$page->setConfig('next','下一页');
		$this->assign('data',$data);*/
		if (!$list) {
			$this->assign ('row','暂无搜索内容...');
		}
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();


	}
	//添加页
	public function add()
	{
		$data = M('category')->field('id,name')->where('pid=2')->select();
		// var_dump($data);exit;
		$this->assign ('data',$data);
		$this->display();
	}
	public function insert()
	{
		$data = $_POST;
		//var_dump($data[3]);exit;
		$data['cid']='';
		for ($i = 1;$i <14;$i ++) {
			if (!empty($data[$i])){
				$data['cid'].=$data[$i].',';
			}
		}
		$data['cid'] = rtrim($data['cid'],',');
		//var_dump($data);
		$data['showdate'] = strtotime($data['showdate']);
		$movie = D('movie');
		if (!$movie->create($data))
		{
			$this->error($movie->getError());
		} else
		{
			//var_dump($movie->create($data));exit;
			$data = $movie->add();
			// echo M('movie')->getLastSql();
			/*var_dump($data);
			exit;*/
			if ($data > 0)				
			{
				$this->success('添加成功',U('Movie/index'));
			} else
			{
				$this->error('添加失败');
			}
		}
	}
	//编辑页
	public function edit()
	{
		$id= I('get.id/d');
		/*var_dump($id);exit;*/
		$data = M('movie')->find($id);
		$this->assign('list',$data);
		$this->display('Movie/edit');
	}
	public function update()
	{
		$data = $_POST;
		//var_dump($data[3]);exit;
		$data['cid']='';
		for ($i = 1;$i <14;$i ++) {
			if (!empty($data[$i])){
				$data['cid'].=$data[$i].',';
			}
		}
		$data['cid'] = rtrim($data['cid'],',');
		//var_dump($data);
		$data['showdate'] = strtotime($data['showdate']);
		$movie = D('movie');
		if(!$movie->create($data))
		{
			$this->error($movie->getError());
		} else {
			/*var_dump($movie->create());
			$data = $movie->save();
			// echo M('movie')->getLastSql();
			var_dump($data);*/
			if ($movie->save()!==false)
			{
				$this->success('修改成功',U('index'));
			} else {
				$this->error('修改失败');
			}
		}
	}
	public function update1 ()
	{
		//var_dump($_GET);exit;
		/*$movie = D ('movie');
		if (!$movie->create($_GET)) {
			$this->error ($movie->getError());
		} else {
			if ($movie->save()) {
				if($_GET['display']==1) {
					echo '隐藏';
				} else {
					echo '显示';
				}
			}
		}*/
		$id = I('get.id/d');
		$display = M('movie')->where('id='.$id)->find();
		$display = $display['display'];
		//var_dump($display);exit;
		switch ($display) {
			case '1':
			$data['id'] = $id;
			$data['display'] = 0;
			$movie = D ('movie');
		if (!$movie->create ($data)) {
			$this->error ($movie->getError());
		} else {
			if ($movie->save()!==false) {
				
					echo '显示';
				}
			
		}
			break;
			case '0':
			$data['id'] = $id;
			$data['display'] = 1;
			$movie = D ('movie');
		if (!$movie->create ($data)) {
			$this->error ($movie->getError());
		} else {
			if ($movie->save()!==false) {
				
					echo '隐藏';
				}
			
		}
			break;

		}
	}
	//删除
	public function delete()
	{
		$id = I('get.id/d');
		if (M('movie')->delete($id) > 0) {
			$this->success('删除成功',U('index'));
		} else {
			$this->error('删除失败');
		}
	}
	
}